package vue;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class VueJeu1 extends JPanel implements VueJeu {

    private JLabel j_aff, j_score, j_keyboard1, j_keyboard2, j_combo;
    private JButton j_vrai, j_faux, j_rejouer, j_menu;
    private Integer lastInput;
    private JPanel jp, jp2;

    public VueJeu1() {

        j_rejouer = new JButton("Play Again");
        j_rejouer.setVisible(false);
        j_menu = new JButton("Main menu");
        j_menu.setVisible(false);

        j_vrai = new JButton(" TRUE ");
        j_aff = new JLabel();
        j_aff.setForeground(Color.BLUE);

        j_score = new JLabel("SCORE : 0");
        j_faux = new JButton(" FALSE ");

        j_keyboard1 = new JLabel("Q=true ");
        j_keyboard2 = new JLabel("P=false");
        j_combo = new JLabel("COMBO : x1.0");

        this.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1;
        gbc.weighty = 1;

        gbc.gridx = 0;
        gbc.gridwidth = 3;
        gbc.gridheight = 1;
        gbc.gridy = 0;
        jp = new JPanel();
        GridLayout g = new GridLayout(1, 2);
        g.setHgap(100);
        jp.setLayout(g);
        jp.add(j_vrai);
        jp.add(j_aff);
        jp.add(j_faux);
        this.add(jp, gbc);

        gbc.gridx = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.gridy = 1;
        jp2 = new JPanel();
        GridLayout g2 = new GridLayout(2, 2);
        g2.setHgap(20);
        jp2.setLayout(g2);
        jp2.add(j_score);
        jp2.add(j_combo);
        jp2.add(j_keyboard1, gbc);
        jp2.add(j_keyboard2, gbc);
        this.add(jp2, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.gridy = 1;
        this.add(j_rejouer, gbc);

        gbc.gridx = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.gridy = 1;
        this.add(j_menu, gbc);

        this.affecterCouleur(new Color(215, 255, 175));

        this.lastInput = null;

        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getID() == KeyEvent.KEY_TYPED) {
                    char c = e.getKeyChar();
                    VueJeu1.this.inputUtilisateur(c);
                }
                return false;
            }
        });

        j_vrai.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                VueJeu1.this.inputUtilisateur('q');
            }
        });

        j_rejouer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame f1 = (JFrame) SwingUtilities.windowForComponent(VueJeu1.this);
                Rectangle r = f1.getBounds();
                ThreadVue tv = new ThreadVue(r);
                tv.start();
                f1.dispose();
                System.gc();

            }
        });

        j_menu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame f1 = (JFrame) SwingUtilities.windowForComponent(VueJeu1.this);
                Rectangle r = f1.getBounds();
                ThreadMenu tm = new ThreadMenu(r);
                tm.start();
                f1.dispose();
                System.gc();

            }
        });

        j_faux.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                VueJeu1.this.inputUtilisateur('p');
            }
        });

        this.repaint();
        this.revalidate();

    }

    public Integer getLastInput() {
        return lastInput;
    }

    public void setLastInput(Integer lastInput) {
        this.lastInput = lastInput;
    }

    public void afficherPhrase(String p) {
        this.j_aff.setText(p);
        this.repaint();
        this.revalidate();
    }

    public void afficherCombo(String s) {
        this.j_combo.setText("COMBO : x" + s);
        if (Double.valueOf(s) == 1.0) {
            this.affecterCouleur(new Color(215, 255, 175));
        } else if (Double.valueOf(s) == 1.2) {
            this.affecterCouleur(new Color(204, 255, 153));
        } else if (Double.valueOf(s) == 1.5) {
            this.affecterCouleur(new Color(178, 255, 102));
        } else {
            this.affecterCouleur(new Color(153, 255, 51));
        }
        this.repaint();
        this.revalidate();
    }

    private void affecterCouleur(Color c) {
        this.setBackground(c);
        this.jp.setBackground(c);
        this.jp2.setBackground(c);
    }

    public void afficherRejouer() {
        this.j_rejouer.setVisible(true);
        this.repaint();
        this.revalidate();
    }

    @Override
    public void afficherMenu() {
        this.j_menu.setVisible(true);
        this.repaint();
        this.revalidate();
    }

    public String saisirPseudo(double d) {

        String saisie = JOptionPane.showInputDialog(this, "Submit your username :", "The game is over : " + String.valueOf((int) d) + " points", -1);
        if (saisie == null || saisie.isEmpty()) {
            saisie = "anonymous";
        }
        return saisie;
    }

    public void inputUtilisateur(char c) {
        if (c == 'q') {
            this.lastInput = 1;

        } else if (c == 'p') {
            this.lastInput = 0;
        } else {

        }
        this.repaint();
        this.revalidate();
    }

    public void afficherScore(double n) {
        this.j_score.setText("SCORE : " + String.valueOf((int) n));
        this.repaint();
        this.revalidate();
    }

    @Override
    public void creerFenetre() {
        // TODO - implement VueJeu1.creerFenetre
        throw new UnsupportedOperationException();
    }

    @Override
    public void run() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
